package com.fenqing.leetcode.code.t500_600;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

/**
 * @author fenqing
 */
public class Topic557 extends Topic {

    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic557(String input) {
        super(input);
    }

    @Execute
    public String reverseWords(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        for (int i = 0; i < length; i++) {
            int index = i;
            while (index + 1 < length && chars[index] != ' '){
                index++;
            };
            int ii = index == length - 1 ? index : index - 1;
            for (int j = i, z = ii; j < z; j++, z--) {
                char temp = chars[j];
                chars[j] = chars[z];
                chars[z] = temp;
            }
            i = index;
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Topic557 topic557 = new Topic557("Let's take LeetCode contest");
        String run = topic557.run();
        System.out.println(run);
    }
}
